<template>
	<a-row class="mb-4">
		<a-col :span="12">
			<a-card :headStyle="{textAlign: 'center'}" class="mr-4" title="礼品卡" bordered hoverable>
				<div style="display: flex;justify-content: center">
					<a-button type="primary" @click="form.onOpen({cardProp: 1})">立即创建</a-button>
				</div>
			</a-card>
		</a-col>
		<a-col :span="12">
			<a-card :headStyle="{textAlign: 'center'}" title="充值卡" bordered hoverable>
				<div style="display: flex;justify-content: center">
					<a-button type="primary" @click="form.onOpen({cardProp: 2})">立即创建</a-button>
				</div>
			</a-card>
		</a-col>

	</a-row>
	<a-card :bordered="false" :body-style="{ 'padding-bottom': '0px' }" class="mb-2">
		<a-form ref="formRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="6">
					<a-form-item label="操作时间：" >
						<div style="display: flex;align-items: center;">
							<a-form-item style="flex: 1;margin-right:12px;" name="startTimeCreated">
								<a-date-picker placeholder="开始日期" class="block" format="YYYY-MM-DD" v-model:value="searchFormState.startTimeCreated" allow-clear/>
							</a-form-item>
							<a-form-item style="flex: 1" name="endTimeCreated">
								<a-date-picker placeholder="结束日期" class="block" format="YYYY-MM-DD" v-model:value="searchFormState.endTimeCreated" allow-clear/>
							</a-form-item>
						</div>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="操作单号：" name="orderNo">
						<a-input v-model:value="searchFormState.orderNo" placeholder="请输入操作单号" allow-clear></a-input>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="卡类型：" name="cardType">
						<a-select :options="cardTypeOptions" placeholder="请选择卡类型" v-model:value="searchFormState.cardType" allow-clear></a-select>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-space>
						<a-button type="primary" html-type="submit" @click="table.refresh()">查询</a-button>
						<a-button @click="() => formRef.resetFields()">重置</a-button>
					</a-space>
				</a-col>
			</a-row>
		</a-form>
	</a-card>
	<a-card :bordered="false">
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'icon'">
					<component :is="record.icon" />
				</template>
				<template v-if="column.dataIndex === 'cardType'">
					{{ $TOOL.tableData('CRM_CARD_TYPE', record.cardType) }}
				</template>
				<template v-if="column.dataIndex === 'store'">
					{{ $TOOL.tableData('ORG_CATEGORY', record.storeCode, 'code') }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<template v-if="record.status === 0">
							<a-button size="small" type="link" @click="form.onOpen(record)">编辑</a-button>
							<a-divider type="vertical" />
						</template>
						<a-button size="small" type="link" @click="form.onOpen(record, true)">详情</a-button>
						<a-divider type="vertical" />
						<a-dropdown :trigger="['click']">
							<a-button size="small" type="link" @click.prevent>
								更多
								<DownOutlined />
							</a-button>
							<template #overlay>
								<a-menu>
									<a-menu-item>
										<a-popconfirm :disabled="record.status === 1"  title="确定审批吗？" @confirm="audit(record)">
											<a-button size="small" type="link" :disabled="record.status === 1">审核</a-button>
										</a-popconfirm>
									</a-menu-item>
									<a-menu-item>
										<a-popconfirm :disabled="record.status === 1" title="确定要删除吗？" @confirm="remove(record)">
											<a-button :disabled="record.status === 1"  size="small" type="link" danger>删除</a-button>
										</a-popconfirm>
									</a-menu-item>
								</a-menu>
							</template>
						</a-dropdown>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="form" @successful="table.refresh(true)" />
</template>

<script setup name="createCard">
	import { message } from 'ant-design-vue'
	import crmApi from '@/api/crm/crmApi'
	import tool from '@/utils/tool'
	import Form from './form.vue'
	let searchFormState = reactive({})
	const formRef = ref()
	const table = ref(null)
	let form = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '操作单号',
			dataIndex: 'orderNo',
			width: 140
		},
		{
			title: '发卡门店',
			dataIndex: 'store',
			width: 140
		},
		{
			title: '操作时间',
			dataIndex: 'createdTime',
			sorter: true,
			width: 220,
			align: 'center'
		},
		{
			title: '卡类型',
			dataIndex: 'cardType',
			width: 120,
			align: 'center'
		},
		{
			title: '卡数量',
			dataIndex: 'totalCount',
			width: 150,
			align: 'center'
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: 240
		}
	]
	let selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	let cardTypeOptions = tool
		.tableList('CRM_CARD_TYPE')
		.map((item) => {
			return {
				value: item['id'],
				label: item['name']
			}
		})
	// 列表数据
	const loadData = (parameter) => {
		return crmApi.cardOrderPage(Object.assign(parameter, searchFormState)).then((res) => {
			return res
		})
	}
	const audit = (record) => {
		let params = {
			id: record.id
		}
		crmApi.cardOrderAudit(params).then(() => {
			table.value.refresh(true)
			message.success('审核成功')
		})
	}
	// 删除
	const remove = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		crmApi.removeCardType(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const removeBatch = () => {
		if (selectedRowKeys.value.length < 1) {
			message.warning('请选择一条或多条数据')
			return false
		}
		const params = selectedRowKeys.value.map((m) => {
			return {
				id: m
			}
		})
		crmApi.removeCardType(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
</script>

<style scoped>
	.ant-card-head-title {
		text-align: center !important;
	}
</style>
